package actions;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.DeliveryOrderService;
import domain.DeliveryOrder;
/**
 * @author system auto
 */
public class DeliveryOrderUpdateAction extends Action {
	/**
	 * 用于查询订单.
	 */
	private DeliveryOrderService deliveryOrderService = null;
	/**
	 * 修改订单.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	       HttpServletRequest request, HttpServletResponse response)
	       throws Exception {
	   	// 验证是否是有效的Token,防止重复提交
	   	if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	    	return mapping.findForward("error.duplicateSubmit");
	   	}
		try{
			DynaValidatorForm deliveryOrderForm = (DynaValidatorForm) form;
		 	String id = deliveryOrderForm.getString("id");
			DeliveryOrder deliveryOrder = deliveryOrderService.getDeliveryOrderById(id);
			this.convertBean(deliveryOrder,deliveryOrderForm);
	       	deliveryOrderService.updateDeliveryOrder(deliveryOrder);
	    }catch(Exception e){
	        ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	           // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	    }
	    return mapping.findForward("success");
    }
    /**
     *convertBean
     */
     private void convertBean(DeliveryOrder deliveryOrder, DynaValidatorForm deliveryOrderForm)
	 		throws Exception{
		String orderNumber=deliveryOrderForm.getString("orderNumber");
		deliveryOrder.setOrderNumber(orderNumber);
		String orderType=deliveryOrderForm.getString("orderType");
		deliveryOrder.setOrderType(orderType);
		String vipCustomFlag=deliveryOrderForm.getString("vipCustomFlag");
		deliveryOrder.setVipCustomFlag(vipCustomFlag);
		String sender=deliveryOrderForm.getString("sender");
		deliveryOrder.setSender(sender);
		String fetchAddr=deliveryOrderForm.getString("fetchAddr");
		deliveryOrder.setFetchAddr(fetchAddr);
		String senderPhone=deliveryOrderForm.getString("senderPhone");
		deliveryOrder.setSenderPhone(senderPhone);
		String receiver=deliveryOrderForm.getString("receiver");
		deliveryOrder.setReceiver(receiver);
		String receiverAddr=deliveryOrderForm.getString("receiverAddr");
		deliveryOrder.setReceiverAddr(receiverAddr);
		String receiverPhone=deliveryOrderForm.getString("receiverPhone");
		deliveryOrder.setReceiverPhone(receiverPhone);
		String originLocation=deliveryOrderForm.getString("originLocation");
		deliveryOrder.setOriginLocation(originLocation);
		String destLocation=deliveryOrderForm.getString("destLocation");
		deliveryOrder.setDestLocation(destLocation);
		String chargeWeight=deliveryOrderForm.getString("chargeWeight");
		if(chargeWeight!=null&&!chargeWeight.equals("")){
			deliveryOrder.setChargeWeight(Double.valueOf(chargeWeight));
		}else{
			deliveryOrder.setChargeWeight(null);
		}
		String dimension=deliveryOrderForm.getString("dimension");
		deliveryOrder.setDimension(dimension);
		String contentsDesc=deliveryOrderForm.getString("contentsDesc");
		deliveryOrder.setContentsDesc(contentsDesc);
		String payType=deliveryOrderForm.getString("payType");
		deliveryOrder.setPayType(payType);
		String specialOperateNote=deliveryOrderForm.getString("specialOperateNote");
		deliveryOrder.setSpecialOperateNote(specialOperateNote);
		String insuranceFee=deliveryOrderForm.getString("insuranceFee");
		if(insuranceFee!=null&&!insuranceFee.equals("")){
			deliveryOrder.setInsuranceFee(Double.valueOf(insuranceFee));
		}else{
			deliveryOrder.setInsuranceFee(null);
		}
		String agencyReceiveFee=deliveryOrderForm.getString("agencyReceiveFee");
		if(agencyReceiveFee!=null&&!agencyReceiveFee.equals("")){
			deliveryOrder.setAgencyReceiveFee(Double.valueOf(agencyReceiveFee));
		}else{
			deliveryOrder.setAgencyReceiveFee(null);
		}
		String orderDate=deliveryOrderForm.getString("orderDate");
		if(orderDate!=null&&!orderDate.equals("")){
			DateFormat DATE_FAMAT_ORDERDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
			deliveryOrder.setOrderDate(new Timestamp(DATE_FAMAT_ORDERDATE.parse(orderDate).getTime()));
		}else{
			deliveryOrder.setOrderDate(null);
		}
		String goodsName=deliveryOrderForm.getString("goodsName");
		deliveryOrder.setGoodsName(goodsName);
		String goodsPrice=deliveryOrderForm.getString("goodsPrice");
		if(goodsPrice!=null&&!goodsPrice.equals("")){
			deliveryOrder.setGoodsPrice(Double.valueOf(goodsPrice));
		}else{
			deliveryOrder.setGoodsPrice(null);
		}
		String serviceType=deliveryOrderForm.getString("serviceType");
		deliveryOrder.setServiceType(serviceType);
		String subStation=deliveryOrderForm.getString("subStation");
		deliveryOrder.setSubStation(subStation);
		String creater=deliveryOrderForm.getString("creater");
		deliveryOrder.setCreater(creater);
		String fetcher=deliveryOrderForm.getString("fetcher");
		deliveryOrder.setFetcher(fetcher);
		String dispatcher=deliveryOrderForm.getString("dispatcher");
		deliveryOrder.setDispatcher(dispatcher);
		String problemCode=deliveryOrderForm.getString("problemCode");
		deliveryOrder.setProblemCode(problemCode);
		String scheduler=deliveryOrderForm.getString("scheduler");
		deliveryOrder.setScheduler(scheduler);
		String orderNote=deliveryOrderForm.getString("orderNote");
		deliveryOrder.setOrderNote(orderNote);
		String deliveryFee=deliveryOrderForm.getString("deliveryFee");
		if(deliveryFee!=null&&!deliveryFee.equals("")){
			deliveryOrder.setDeliveryFee(Double.valueOf(deliveryFee));
		}else{
			deliveryOrder.setDeliveryFee(null);
		}
		String pkgCount=deliveryOrderForm.getString("pkgCount");
		if(pkgCount!=null&&!pkgCount.equals("")){
			deliveryOrder.setPkgCount(Integer.valueOf(pkgCount));
		}else{
			deliveryOrder.setPkgCount(null);
		}
     }
   /**
     * @param pDeliveryOrderService the deliveryOrderService to set
     */
    public void setDeliveryOrderService(DeliveryOrderService pDeliveryOrderService) {
        deliveryOrderService = pDeliveryOrderService;
    }
}
